package com.samsungmobile.common_module.Bean;

import java.text.SimpleDateFormat;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringEscapeUtils;

import com.samsungmobile.common.Logger;
import com.samsungmobile.common.util.LocaleUtils;

public class TrackItem extends Item {

	/**
	 * Unique id for track
	 */
	String contentId;
	
	/**
	 * Name of the artist
	 */
	String artist;
	
	/**
	 * Track Name
	 */
	String title;
	
	/**
	 * Size of the File 
	 */
	String fileSize;
	
	/**
	 * Date track was released
	 */
	Date releaseDate;
	
	/**
	 * genreId and genreName describing the genre the track belongs to.
	 */
	String genre;
	
	
	/**
	 * pre-listen source URL
	 */
	String prelisten;
	
	String wapPushURL;

	public String getWapPushURL() {
		return wapPushURL;
	}

	public void setWapPushURL(String wapPushURL) {
		this.wapPushURL = wapPushURL;
	}

	protected String shortArtist = "";
	
	String coverImagePath = "";

	Currency currency = null;
	double price = 0.0;
	
	String priceString;
	int size;
	int sizeKB;
	AlbumItem album = null;
	
	String detailURL = "/entertainment/full-track/album-detail.do";
	String orderURL = "/entertainment/full-track/order.do?cid=";

	public String getOrderURL() {
		return orderURL;
	}
	
	public String getDetailURL() {
		return detailURL;
	}

	public int getSizeKB() {
		return sizeKB;
	}
	
	public void setAlbum(AlbumItem album) {
		this.album = album;
	}
	
	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
		updatePriceString();
	}

	public Currency getCurrency() {
		return currency;
	}

	public void setCurrency(String lang) {

		Locale locale = LocaleUtils.getLocaleByLang(lang);
		this.currency = Currency.getInstance(locale);
		
		updatePriceString();
	}
	
	public void setCurrency(Currency currency) {
		this.currency = currency;
		updatePriceString();
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
		this.sizeKB = size / 1024;
	}

	public String getCoverImagePath() {
		return coverImagePath;
	}

	public void setCoverImagePath(String coverImagePath) {
		this.coverImagePath = coverImagePath;
	}

	public String getShortArtist() {
		return shortArtist;
	}

	public String getPrelisten() {
		return prelisten;
	}

	public void setPrelisten(String prelisten) {
		this.prelisten = prelisten;
	}

	public String getContentId() {
		return contentId;
	}

	public void setContentId(String contentId) {
		this.contentId = contentId;
		this.orderURL = "/entertainment/full-track/order.do?cid="+contentId;
	}

	public String getArtist() {
		return artist;
	}

	public void setArtist(String artist) {
		this.artist = artist;
		this.shortArtist = this.createShortString(this.artist, MaxAuthorLen);
		updateTooltips();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		this.shortTitle = this.createShortString(this.title, MaxTitleLen);
		updateTooltips();
	}

	public String getFileSize() {
		return fileSize;
	}

	public void setFileSize(String fileSize) {
		this.fileSize = fileSize;
	}

	public Date getReleaseDate() {
		return releaseDate;
	}

	//2010-06-27
	public void setReleaseDate(String releaseDate) {
		try {
			SimpleDateFormat sdfmt = new SimpleDateFormat(); 
	    	sdfmt.applyPattern( "yyyy-MM-dd" );  
	    	
	    	this.releaseDate = sdfmt.parse(releaseDate);
		} catch(Exception ex) {
			Logger.LOG_NOHUP.error(ex);
		}
	}
	
	public void setReleaseDate(Date releaseDate) {
		this.releaseDate = releaseDate;
	}

	public String getGenre() {
		return genre;
	}

	public void setGenre(String genre) {
		this.genre = genre;
	}

	@Override
	protected void updateTooltips() {
		imgageTooltip = this.artist + " - " + this.title;
	}
	
	public String getPriceString() {
		return priceString;
	}
	
	private void updatePriceString() {
		try {
			
			String symbol = LocaleUtils.getHtmlCurrencySymbol(currency.getSymbol());
			
			this.priceString =  symbol + " " + this.price;
			
		} catch(Exception ex) {
			Logger.LOG_NOHUP.error(ex);
			this.priceString = "";
		}
	}
	public AlbumItem getAlbum() {
		if(album == null) {
			this.album = new AlbumItem();
		}
		return album;
	}



	@Override
	public void escapeHtml() {
		this.artist = StringEscapeUtils.escapeHtml(this.artist);
		this.title = StringEscapeUtils.escapeHtml(this.title);
		this.genre = StringEscapeUtils.escapeHtml(this.genre);
		this.priceString = StringEscapeUtils.escapeHtml(this.priceString);

		this.album.escapeHtml();
	}
	
	@Override
	public void unescapeHtml() {
		this.artist = StringEscapeUtils.unescapeHtml(this.artist);
		this.title = StringEscapeUtils.unescapeHtml(this.title);
		this.genre = StringEscapeUtils.unescapeHtml(this.genre);
		this.priceString = StringEscapeUtils.unescapeHtml(this.priceString);

		this.album.unescapeHtml();
	}


}
